<?php


namespace app\admin\controller;

use app\admin\validate\AuthValidate;
use app\admin\validate\IDMustBePositiveInt;
use app\service\model\AdminAuthorityModel;
use app\service\Tree;
use app\service\Y;
use think\Request;

class Authority extends BaseAdmin
{
    /**
     * 权限数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function datas(): \think\response\Json
    {
        $datas = AdminAuthorityModel::order('sort', 'desc')->select();
        return Y::table($datas, count($datas));
    }

    /**
     * 保存数据
     * @param Request $request
     * @param AuthValidate $auth_validate
     * @return bool|\think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(Request $request, AuthValidate $auth_validate) {
        $id = input('param.id', 0);
        if ($request->isPost()) {
            $auth_validate->goCheck();
            $param = $auth_validate->getDataByRule();
            $param['name'] = input('param.name', '');
            $param['path'] = input('param.path', '');
            $param['file_path'] = input('param.file_path', '');
            $param['icon'] = input('param.icon', '');
            $param['is_nav'] = input('param.is_nav', 1);
            return AdminAuthorityModel::saveAuth($param, $id);
        }

        $data = [];
        if ($id > 0) {
            $data = AdminAuthorityModel::find($id);
        }
        return Y::success($data, '权限数据');
    }

    /**
     * 获取权限树
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function auth_tree(): \think\response\Json
    {
        if (!$authoritys = cache('auth_tree')) {
            $authoritys = AdminAuthorityModel::order('sort', 'desc')->select();
            $authoritys = Tree::unlimitForLevel($authoritys);
            cache('auth_tree', $authoritys, null, 'admin_cache');
        }
        return Y::success($authoritys, '权限数据');
    }

    /**
     * 删除数据
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        return $this->deleteDate((new AdminAuthorityModel()), $id);
    }

    /**
     * 修改排序
     * @param IDMustBePositiveInt $IDMustBePositiveInt
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save_sort(IDMustBePositiveInt $IDMustBePositiveInt): \think\response\Json
    {
        $IDMustBePositiveInt->goCheck();
        $id = input('param.id');
        $data = AdminAuthorityModel::find($id);
        if ($data) {
            $data->save(['sort' => input('param.value', 0)]);
        }
        return Y::success([], '修改成功');
    }
}